package com.example.administrator.testfragment;

import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.testfragment.fragment.HomeFragment;
import com.example.administrator.testfragment.fragment.LikeFragment;
import com.example.administrator.testfragment.fragment.LocationFragment;
import com.example.administrator.testfragment.fragment.PersonFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private TextView mTHome, mTLocation, mTLike, mTMe;
    private HomeFragment mHomeFragment;
    private LocationFragment mLocationFragment;
    private LikeFragment mLikeFragment;
    private PersonFragment mPersonFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mTHome.setOnClickListener(this);
        mTLocation.setOnClickListener(this);
        mTLike.setOnClickListener(this);
        mTMe.setOnClickListener(this);
    }

    private void initData() {
        setDefaultFragment();
    }

    /**
     * set the default Fragment 设置默认碎片
     */
    private void setDefaultFragment() {
        switchFragment(0);
        //set the default tab state  设置默认选项卡状态
        setTabState(mTHome, R.drawable.home_fill, getColors(R.color.colorPrimary));
    }

    private void initView() {
        mTHome = findViewById(R.id.tv_home);
        mTLocation = findViewById(R.id.tv_location);
        mTLike = findViewById(R.id.tv_like);
        mTMe = findViewById(R.id.tv_person);
    }

    @Override
    public void onClick(View v) {
        resetTabState();//reset the tab state 重置选项卡状态
        switch (v.getId()) {
            case R.id.tv_home:
                setTabState(mTHome, R.drawable.home_fill, getColors(R.color.colorPrimary));
                switchFragment(0);
                break;
            case R.id.tv_location:
                setTabState(mTLocation, R.drawable.location_fill, getColors(R.color.colorPrimary));
                switchFragment(1);
                break;
            case R.id.tv_like:
                setTabState(mTLike, R.drawable.like_fill, getColors(R.color.colorPrimary));
                switchFragment(2);
                break;
            case R.id.tv_person:
                setTabState(mTMe, R.drawable.person_fill, getColors(R.color.colorPrimary));
                switchFragment(3);
                break;
        }
    }

    /**
     * switch the fragment accordting to id
     *
     * @param i id
     */
    private void switchFragment(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (i) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                }
                transaction.replace(R.id.sub_content, mHomeFragment);
                break;
            case 1:
                if (mLocationFragment == null) {
                    mLocationFragment = new LocationFragment();
                }
                transaction.replace(R.id.sub_content, mLocationFragment);
                break;
            case 2:
                if (mLikeFragment == null) {
                    mLikeFragment = new LikeFragment();
                }
                transaction.replace(R.id.sub_content, mLikeFragment);
                break;
            case 3:
                if (mPersonFragment == null) {
                    mPersonFragment = new PersonFragment();
                }
                transaction.replace(R.id.sub_content, mPersonFragment);
                break;
        }
        transaction.commit();
    }

    /**
     * set the tab state of bottom navigation bar
     *
     * @param textView the text to be shown
     * @param image    the image
     * @param color    the text color
     */
    private void setTabState(TextView textView, int image, int color) {
        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, image, 0, 0);//Call requires API level 17
        textView.setTextColor(color);
    }


    /**
     * revert the image color and text color to black
     */
    private void resetTabState() {
        setTabState(mTHome, R.drawable.home, getColors(R.color.black_1));
        setTabState(mTLocation, R.drawable.location, getColors(R.color.black_1));
        setTabState(mTLike, R.drawable.like, getColors(R.color.black_1));
        setTabState(mTMe, R.drawable.person, getColors(R.color.black_1));

    }


    public int getColors(int id) {
        return ContextCompat.getColor(MainActivity.this, id);
    }
}
